
package vue.panels;

import javax.swing.*;
import vue.Ui;
import vue.components.*;
import controler.Joueur;

import java.awt.event.*;
/**
 * <p>Title: vue.LoginPanel </p>
 *
 * <p>
 * 	Description: Cette classe gere l'affichage du panneau de login
 * </p>
 *
 * <p>Copyright: (c) 2008 FSSIM</p>
 *
 * <p>Company: FSSImock</p>
 *
 * @author Antoine Farnault
 * @author Augustin Couette
 * @version 1.0
 */
public class LoginPanel extends TPPanel {

	private static final long serialVersionUID = 1L;
	private Ui ui; // passage par reference : aggregation 
	
    // ++ Composants graphiques
    private TPButton okButton;
    private TPLabel passLabel;
    public TPPassewordField passTextField;
    private TPLabel pseudoLabel;
    public TPTextField pseudoText;
    // -- Composants graphiques
    
	/** Creates new form LoginPanel */
    public LoginPanel(Ui ui) {
    	this.ui = ui;
        initComponents();
    }
    
    /** 
     * This method is called from within the constructor to
     * initialize the form.
     */
    private void initComponents() {
    	setName("loginPanel");
    	//ResourceBundle bundle = ResourceBundle.getBundle("locales/fr_FR");
    	
    	//setBorder(new TitledBorder(null, bundle.getString("CONNEXION_TITLE"),TitledBorder.DEFAULT_JUSTIFICATION, TitledBorder.DEFAULT_POSITION, null, null));
        setTPTitle("J'ai deja un compte");
        pseudoLabel   = new TPLabel();
        passLabel     = new TPLabel();
        pseudoText    = new TPTextField();
        passTextField = new TPPassewordField();
        okButton      = new TPButton();
        
        //pseudoLabel.setText(bundle.getString("CONNEXION_LOGIN_LABEL"));
        pseudoLabel.setText("Pseudo");
        //passLabel.setText(bundle.getString("CONNEXION_PASS_LABEL"));
        passLabel.setText("Mot de passe");
        
        pseudoText.addKeyListener(new KeyAdapter(){
        	public void keyPressed(KeyEvent e){
        		enterKeyPressed(e);
        	}
        });
        
        passTextField.addKeyListener(new KeyAdapter(){
        	public void keyPressed(KeyEvent e){
        		enterKeyPressed(e);
        	}
        });
        
        //okButton.setText(bundle.getString("CONNEXION_OK_BUTTON"));
        okButton.setText("Connexion");
        okButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent evt) {
                okButtonActionPerformed(evt);
            }
        });
        okButton.addKeyListener(new KeyAdapter(){
        	public void keyPressed(KeyEvent e){
        		enterKeyPressed(e);
        	}
        });
        
        GroupLayout layout = new GroupLayout(this);
        setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(GroupLayout.Alignment.TRAILING, false)
                    .addComponent(okButton, GroupLayout.Alignment.LEADING, GroupLayout.DEFAULT_SIZE, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addGroup(GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(GroupLayout.Alignment.LEADING)
                            .addComponent(passLabel)
                            .addComponent(pseudoLabel))
                        .addPreferredGap(LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(GroupLayout.Alignment.LEADING, false)
                            .addComponent(passTextField)
                            .addComponent(pseudoText, GroupLayout.DEFAULT_SIZE, 150, Short.MAX_VALUE))))
                .addContainerGap(31, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(GroupLayout.Alignment.BASELINE)
                    .addComponent(pseudoLabel)
                    .addComponent(pseudoText, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(GroupLayout.Alignment.BASELINE)
                    .addComponent(passLabel)
                    .addComponent(passTextField, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addComponent(okButton)
                .addContainerGap(33, Short.MAX_VALUE))
        );
    }
    
    /**
     * Appuie sur entree sur l'un des composants
     * @param e
     */
    private void enterKeyPressed(KeyEvent e){
    	if(e.getKeyCode() == 10){ // Touche entree
    		okButtonActionPerformed(null);
    	}
    }
    
    /**
     * Clique sur le bouton ok
     * @param evt
     */
    private void okButtonActionPerformed(ActionEvent evt) {
    	Joueur.name = pseudoText.getText();
    	Joueur.ui   = ui;
        String tmp = "";
        for(int i = 0; i < passTextField.getPassword().length; i++){
        	tmp += passTextField.getPassword()[i];
        }
        
        //connexion du joueur
       Joueur.connect(pseudoText.getText(), tmp); 
    }
}
